package cn.blnp.net.flowable.boot.ext.producer;

import cn.blnp.net.flowable.boot.bean.dto.HistoricProcessInstanceEntityMessage;
import cn.blnp.net.flowable.boot.bean.enums.OpType;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.flowable.common.engine.impl.persistence.entity.Entity;
import org.flowable.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <h3>延时历史数据转发，数据一致性保证</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/26 10:40
 */
@Slf4j
@Component
public class HistoricDataCompensationProducer {

    @Resource
    private RocketMQTemplate rocketMQTemplate;
    public static final String HISTORIC_TOPIC = "historic_mq_topic";

    public void sendJobMessage(OpType opType, HistoricProcessInstanceEntity entity) {
        log.info("Producer historic variable message,processInstanceId={},variable={}", entity);
        HistoricProcessInstanceEntityMessage entityMessage = new HistoricProcessInstanceEntityMessage(opType, entity, System.currentTimeMillis());
        Message<HistoricProcessInstanceEntityMessage> message = MessageBuilder.withPayload(entityMessage).build();
        rocketMQTemplate.syncSend(HISTORIC_TOPIC, message, 1000, 4);
    }
}
